package com.pacific.sign.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pacific.common.exception.BizException;
import com.pacific.sign.dal.dao.pac.custom.MenuCustom;
import com.pacific.sign.model.po.pac.mbg.PacHcGroup;
import com.pacific.sign.model.po.pac.mbg.PacHcMenu;
import com.pacific.sign.model.po.pac.mbg.PacHcRole;
import com.pacific.sign.model.po.pac.mbg.PacHcRoleMenu;
import com.pacific.sign.model.po.pac.request.PacHcMenuQueryRequest;
import com.pacific.sign.model.po.pac.request.PacHcMenuRequest;
import com.pacific.sign.model.po.pac.response.PacHcMenuQueryResponse;
import com.pacific.sign.model.po.pac.response.PacHcMenuResponse;
import com.pacific.sign.model.po.pac.response.PacHcRoleMenuResponse;
import com.pacific.sign.model.po.pac.response.model.PacHcMenuModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单
 */
@Service
public class MenuService {

    @Autowired
    private MenuCustom menuCustom;
    @Autowired
    private GroupService groupService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private RoleService roleService;

    /**
     * 查询
     */
    public PacHcMenuQueryResponse query(PacHcMenuQueryRequest req){
        PacHcMenuQueryResponse resp = new PacHcMenuQueryResponse();
         /*	1.	分页请求	*/
        PageHelper.startPage(
                req.getPageNum() == null ? 1 : req.getPageNum(),
                req.getPageSize() == null ? 20 : req.getPageSize()
        );
        PacHcMenu model = new PacHcMenu();
        model.setId(req.getId());
        model.setParentId(req.getParentId());
        model.setLevel(req.getLevel());
        List<PacHcMenu> list = menuCustom.selectByConditions(model);
         /* 2. 分页返回 */
        PageInfo pageInfo = new PageInfo(list);
        resp.setPageNum(pageInfo.getPageNum());
        resp.setPageSize(pageInfo.getPageSize());
        resp.setPages(pageInfo.getPages());
        resp.setTotal(pageInfo.getTotal());
        List<PacHcMenuModel> respList = new ArrayList<PacHcMenuModel>();
        if(list != null && list.size() > 0){
            for(PacHcMenu menu : list){
                PacHcMenuModel ph = new PacHcMenuModel();
                ph.setId(menu.getId());
                ph.setIcon(menu.getIcon());
                ph.setLevel(menu.getLevel());
                ph.setName(menu.getName());
                ph.setParentId(menu.getParentId());
                ph.setUrl(menu.getUrl());
                if("1".equals(req.getLevel())){
                    ph.setLevelStr("一级菜单");
                }else{
                    ph.setLevelStr("二级菜单");
                }
                PacHcMenu pacHcMenu = menuCustom.selectById(String.valueOf(menu.getParentId()));
                if(pacHcMenu != null){
                    ph.setParentIdStr(pacHcMenu.getName());
                }

                respList.add(ph);
            }
        }

        resp.setModels(respList);

        return resp;
    }


    public int insert(PacHcMenu obj){
        int i = menuCustom.insert(obj);
        if(i <1){
            throw new BizException("新增失败");
        }
        return i;
    }

    public int update(PacHcMenu obj){
        int i = menuCustom.updateById(obj);
        if(i <1){
            throw new BizException("更新失败");
        }
        return i;
    }

    public int delete(PacHcMenu obj){
        if(obj == null || obj.getId() == null){
            throw new BizException("主键为空");
        }
        int i = menuCustom.deleteById(String.valueOf(obj.getId()));
        if(i <1){
            throw new BizException("删除失败");
        }
        return i;
    }

    public PacHcMenu selectById(Integer id){
        if(id == null){
            throw new BizException("主键为空");
        }
        return menuCustom.selectById(String.valueOf(id));
    }


    /**
     * 查询菜单
     */
    public PacHcMenuResponse queryMenu(PacHcMenuRequest req){
        PacHcMenuResponse resp = new PacHcMenuResponse();

        //菜单
        String htmlStr = "[{'title':'后台首页','icon':'larry-houtaishouye','href':'../main'},";
        //按钮权限
        String roleStr = "";


        //加载事业部菜单（去掉此功能注释掉本行即可）
        htmlStr +=  businessMenu(req.isBusiness());

        PacHcGroup pacHcGroup = groupService.queryByCode(req.getCode());
        if(pacHcGroup != null){

            /**
             * 1.菜单
             */
            PacHcRoleMenu roleMenu = new PacHcRoleMenu();
            roleMenu.setRoleId(pacHcGroup.getId());
            roleMenu.setMenuLevel("1");//1级菜单
            roleMenu.setType("1");
            PacHcRoleMenuResponse resp1 = roleMenuService.query(roleMenu);
            if(resp1.getList() != null && resp1.getList().size() > 0){
                for(PacHcRoleMenu menu : resp1.getList()){
                    //查询菜单表
                    PacHcMenu pacHcMenu = menuCustom.selectById(String.valueOf(menu.getMenuId()));
                    htmlStr +="{'title':'"+ pacHcMenu.getName() +"','icon':'"+ pacHcMenu.getIcon() +"','spread':'true','children':[";
                    if(pacHcMenu != null){
                        PacHcRoleMenu roleMenu2 = new PacHcRoleMenu();
                        roleMenu2.setRoleId(pacHcGroup.getId());
                        roleMenu2.setParentId(menu.getMenuId());
                        roleMenu2.setMenuLevel("2");//2级菜单
                        roleMenu2.setType("1");//菜单
                        PacHcRoleMenuResponse resp2 = roleMenuService.query(roleMenu2);
                        if(resp2.getList() != null && resp2.getList().size() > 0){
                            for(PacHcRoleMenu menu2 : resp2.getList()){
                                PacHcMenu pacHcMenu2 = menuCustom.selectById(String.valueOf(menu2.getMenuId()));
                                htmlStr +="{'title':'"+ pacHcMenu2.getName() +"','icon':'"+ pacHcMenu2.getIcon()+"','href':'"+ pacHcMenu2.getUrl() +"'},";
                            }
                            htmlStr = htmlStr.substring(0,htmlStr.length() - 1);
                        }

                    }
                    htmlStr +="]},";
                }


                /**
                 * 权限
                 */
                PacHcRoleMenu role = new PacHcRoleMenu();
                role.setRoleId(pacHcGroup.getId());
                role.setType("2");//权限
                PacHcRoleMenuResponse resp2 = roleMenuService.query(role);
                if(resp2.getList() != null && resp2.getList().size() > 0){
                    for(PacHcRoleMenu rm :resp2.getList()){
                        PacHcRole pacHcRole = roleService.selectById(rm.getMenuId());
                        if(pacHcRole != null){
                            roleStr += pacHcRole.getCode() +",";
                        }
                    }
                    roleStr = roleStr.substring(0,roleStr.length() - 1);
                }




            }
            if(!StringUtils.isBlank(htmlStr))
                htmlStr = htmlStr.substring(0,htmlStr.length() - 1);
        }

        htmlStr +="]";
        resp.setHtmlStr(htmlStr);
        resp.setRoleStr(roleStr);
        return resp;
    }


    /**
     * 加载事业部菜单
     */
    private String businessMenu(boolean isBusiness){
        String menuStr = "";
        if(isBusiness){
            PacHcGroup pacHcGroup = groupService.queryByCode("0001");//事业部
            if(pacHcGroup != null){

                PacHcRoleMenu roleMenu = new PacHcRoleMenu();
                roleMenu.setRoleId(pacHcGroup.getId());
                roleMenu.setMenuLevel("1");//1级菜单
                roleMenu.setType("1");
                PacHcRoleMenuResponse resp1 = roleMenuService.query(roleMenu);
                if(resp1.getList() != null && resp1.getList().size() > 0){
                    for(PacHcRoleMenu menu : resp1.getList()){
                        //查询菜单表
                        PacHcMenu pacHcMenu = menuCustom.selectById(String.valueOf(menu.getMenuId()));
                        menuStr +="{'title':'"+ pacHcMenu.getName() +"','icon':'"+ pacHcMenu.getIcon() +"','spread':'true','children':[";
                        if(pacHcMenu != null){
                            PacHcRoleMenu roleMenu2 = new PacHcRoleMenu();
                            roleMenu2.setRoleId(pacHcGroup.getId());
                            roleMenu2.setParentId(menu.getMenuId());
                            roleMenu2.setMenuLevel("2");//2级菜单
                            roleMenu2.setType("1");//菜单
                            PacHcRoleMenuResponse resp2 = roleMenuService.query(roleMenu2);
                            if(resp2.getList() != null && resp2.getList().size() > 0){
                                for(PacHcRoleMenu menu2 : resp2.getList()){
                                    PacHcMenu pacHcMenu2 = menuCustom.selectById(String.valueOf(menu2.getMenuId()));
                                    menuStr +="{'title':'"+ pacHcMenu2.getName() +"','icon':'"+ pacHcMenu2.getIcon()+"','href':'"+ pacHcMenu2.getUrl() +"'},";
                                }
                                menuStr = menuStr.substring(0,menuStr.length() - 1);
                            }
                        }
                        menuStr +="]},";
                    }
                }
            }
        }


        return menuStr;
    }
}
